<?php
/**
 * Created by JetBrains PhpStorm.
 * User: fanzhanao
 * Date: 11-4-13
 * Time: 下午4:53
 * To change this template use File | Settings | File Templates.
 */
 
class member_util_avatar{

    public $data;
	public $status;
	public $statusText;

//    private $MID = array('width'=>120,'heigth'=>120);

    public function __construct(){
        $this->data->urls = array();
    }

    /**
     * @desc  上传用户头像
     * @param  $uid 用户uid
     * @return void
     */
    public  function get_avatar_path($uid,$type='small'){
        if(!$uid) return false;
        $file_path = self::get_real_path($uid,$type);
        if(!is_dir($file_path)){
            $file_path = cartoon_util_cache::mkdirs($file_path);
        }
        return $file_path.self::get_filename($uid,$type);
    }

    /**
     * @desc	获取用户头像
     * @param  INT $uid		用户uid
     * @param string $type	头像类别
     * @return void
     */
    public function get_avatar($uid,$type = 'small',$default = false){
        if(!$uid) return false;
        $userinfo = member_util_login::getMemberInfo($uid,'headimg');
        if($userinfo['headimg'] == 0){
        	if(!in_array($type,array('small','mid','big'))) $type = 'small';
        	return '/images/avatar/avatar_'.$type.'.jpg'; 
        }
        return CT_IMG_DOMAIN.'/avatar/'.self::get_directory($uid).'/'.self::get_filename($uid,$type);
    }
    
	/**
     * 上传图片
     */
    public  function upload_photo($uid,$file){
    	if(!$uid) return false;
    	$file_path = 'avatar/origin/';
    	if(!is_dir($file_path)){
    		cartoon_util_cache::mkdirs(DOCUMENT_PATH.'/sns/temp/' . $file_path);
    	}
    	$file_name = 'avatar_'.$uid;
    	$_file = $_FILES[$file];
    	/**
    	 * 判断是否是jpeg类型图片，如果不是，返回错误
    	 */
    	$type = exif_imagetype($_file['tmp_name']);
			if($type != IMAGETYPE_JPEG){
				return array('result'=>false,'message'=>cartoon_util_upload::ERROR_FILETYPE);
		}
    	return  cartoon_util_upload::upload_file($file,$file_path,$file_name,false,DOCUMENT_PATH.'/sns/temp/');
    	
    }

    /**
	 * 根据uid 和尺寸得到相应的图片url
	 *
	 * @param int $uid
	 * @param int $size
	 * @return string
	 */
	public  function get_path($uid, $size=120){
		return $_SERVER['config']['avatar']['path']
			.self::get_directory($uid)."/"
			.self::get_filename($uid, $size);
	}

    /**
	 * 获得图片磁盘目录路径
	 *
	 * @param int $uid
	 * @param string $root_path
	 * @return string
	 */
	public  function get_real_path($uid, $root_path){
		return $_SERVER['config']['avatar']['path']
			.self::get_directory($uid)."/";
	}

    /**
	 * 获得图片的相对路径
	 *
	 * @param int $uid
	 * @return string
	 */
	public  function get_directory($uid){
		$hex = dechex($uid);
		$hex = str_repeat('0', 8-strlen($hex)).$hex;
		$regex = '`([\da-z]{2})([\da-z]{2})([\da-z]{2})([\da-z]{2})`';
		preg_match($regex, $hex, $match);
		array_pop($match);
		array_shift($match);
		return implode($match, '/');
	}

    /**
	 * 获得图片文件名
	 *
	 * @param int $uid
	 * @param int $size
	 * @return string
	 */
	public  function get_filename($uid, $size=120){
		return $size."_".$uid.".jpg";
	}

    /**
	 * 本地历史图片改名
	 *
	 * @param int $uid
	 * @return void
	 */
	public  function rename_local($uid, $root_path){
		$sizes = self::$SIZES;
		$dir = self::get_real_path($uid, $root_path);
		$files = glob($dir."120_${uid}.*.jpg");
		$regex = '`(.*)\.(.*)\.jpg`';
		$max = 0;
		foreach($files as $file){
			preg_match($regex, $file, $match);
			$index = intval($match[2]);
			if($index>$max){
				$max = $index;
			}
		}
		$max += 1;
		foreach($sizes as $size){
			rename($dir."${size}_${uid}.jpg", $dir."${size}_${uid}.${max}.jpg");
		}
	}

    /**
	 * 裁剪图片
	 *
	 * @param unknown_type $source		原始图
	 * @param unknown_type $dest		存放的目标图片
	 */
	public function cutImage($source,$dest=NULL){
		try{
			$dest = $dest?$dest:$source;
			$cute = new cartoon_util_resize($source);
			$ext = pathinfo($dest, PATHINFO_EXTENSION);
			$file_path = substr($dest,0,strpos($dest,$ext)-1);
			//大图
			if(self::$SIZES['big']['width']>0){
				$file_name = $file_path."_big.".$ext;
				$cute->setNewImage($file_name);
				$cute->setNewSize(self::$SIZES['big']['heigth'],self::$SIZES['big']['width']);
				$cute->make();
			}

			/**
			 * 中图
			 */

			if(self::$SIZES['mid']['width']>0){
				$file_name = $file_path."_mid.".$ext;
				$cute->setNewImage($file_name);
				$cute->setNewSize(self::$SIZES['mid']['heigth'],self::$SIZES['mid']['width']);
				$cute->make();
			}

			/**
			 * 小图
			 */

			if(self::$SIZES['small']['width']>0){
				$file_name = $file_path."_small.".$ext;
				$cute->setNewImage($file_name);
				$cute->setNewSize(self::$SIZES['small']['heigth'],self::$SIZES['small']['width']);
				$cute->make();
			}
		}
		catch (Exception $e){
			echo $e;
		}
	}

}